package itstudio.instructor.ui;

import org.apache.http.Header;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import itstudio.app.wq.R;
import itstudio.instructor.config.Config;
import itstudio.instructor.http.TwitterRestClient;
import itstudio.instructor.util.AESCipher;
import itstudio.instructor.util.LoginUtil;
import itstudio.instructor.util.SharedPreferencesUtil;
import itstudio.instructor.util.ToastUtil;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RevisePsw extends Activity{
	
	private View back_layout;
	private EditText psw_pre_et;
	private EditText psw_new_et;
	private EditText psw_confirm_et;
	private Button save_btn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_revise_psw);
		
		back_layout=findViewById(R.id.back_layout);
		psw_pre_et=(EditText) findViewById(R.id.psw_et);
		psw_new_et=(EditText) findViewById(R.id.psw_new_et);
		psw_confirm_et=(EditText) findViewById(R.id.psw_confirm_et);
		save_btn=(Button) findViewById(R.id.save_btn);
		
		back_layout.setOnClickListener(onclick);
		save_btn.setOnClickListener(onclick);
	}
	
    OnClickListener onclick = new   OnClickListener() {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.back_layout:
				finish();
				break;
			case R.id.save_btn:
				saveInfo();
				break;
	
			default:
				break;
			}
		}
	};
	
	private void saveInfo() {
		// TODO Auto-generated method stub
		// 数据为null 原始密码格式校验
		if(TextUtils.isEmpty(psw_pre_et.getText() )||
				TextUtils.isEmpty(psw_new_et.getText())||
				TextUtils.isEmpty(psw_confirm_et.getText())||
				psw_pre_et.getText().length()<6||
				psw_pre_et.getText().length()>12
				){
			ToastUtil.showErrorMsg(RevisePsw.this, R.string.error_data_null);
			return;
		}
		// 密码不一致
		if(!psw_new_et.getText().toString().equals(psw_confirm_et.getText().toString())){
			
			ToastUtil.showErrorMsg(RevisePsw.this, R.string.error_password_not_same);
			return;
		}
		// 新密码格式校验
		if(psw_new_et.getText().length()<6 || psw_new_et.getText().length()>12 ){
			ToastUtil.showErrorMsg(RevisePsw.this, R.string.error_password_format);
			return;
		}
		// 上面的校验  密码格式已经正确
		httpSaveInfo(psw_pre_et.getText().toString(),psw_new_et.getText().toString());
	}
	
	private void httpSaveInfo(String password,final String  newPassword){
		
		save_btn.setText(R.string.saving);
		RequestParams params = new RequestParams();
		params.put("user.password",password);
		params.put("newPassword", newPassword);
		TwitterRestClient.post(Config.AC_USER_PASSWORD , params, new AsyncHttpResponseHandler(){

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				save_btn.setText(R.string.save);
				ToastUtil.showErrorMsg(RevisePsw.this, R.string.fail_email_save);
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				//加载成功
				save_btn.setText(R.string.save);
				String result = new String (arg2);
				if(result.indexOf(Config.SUCCESS)!=-1){
					ToastUtil.showSuccessMsg(RevisePsw.this, R.string.success_password_save);
					String password = AESCipher.encrypt(AESCipher.key, newPassword);
					SharedPreferencesUtil.add(RevisePsw.this, "password",password);
				}
				if(result.indexOf(Config.FAIL)!=-1){
					ToastUtil.showErrorMsg(RevisePsw.this, R.string.fail_password_save);
				}
				if(result.indexOf(Config.LOGIN)!=-1){
					//掉线了
					LoginUtil.Login(RevisePsw.this);
				}
				if(result.indexOf(Config.ERROR)!=-1){
					//掉线了
					ToastUtil.showErrorMsg(RevisePsw.this, R.string.fail_password_pre);
				}
		}});
	}

}
